package com.lear.rrbus.utils;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lear.rrbus.R;
import com.lear.rrbus.data.StationInfo;
import com.lear.rrbus.view.LearWebView;

public class StationInfoAdapter extends ArrayAdapter<StationInfo> {

	private class URLSpanNoUnderline extends URLSpan {
		public URLSpanNoUnderline(String url) {
			super(url);
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			super.updateDrawState(ds);
			ds.setUnderlineText(false);
		}
	}

	private int resources;
	private TextView indexView;
	private TextView nameView;
	private TextView timeView;
	private Context mContext;
	private LinearLayout stationLayout;
	private LayoutInflater inflater;

	public StationInfoAdapter(Context context, int resource,
			List<StationInfo> objects) {
		super(context, resource, objects);
		this.resources = resource;
		this.mContext = context;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(resources, null);

		indexView = (TextView) convertView.findViewById(R.id.tx_count);
		nameView = (TextView) convertView.findViewById(R.id.tx_name);
		timeView = (TextView) convertView.findViewById(R.id.tx_time);
		stationLayout = (LinearLayout) convertView
				.findViewById(R.id.station_layout);
		stationLayout.setTag(position);

		StationInfo bus = (StationInfo) this.getItem(position);

		SharedPreferences prefs = mContext.getSharedPreferences(Utils.APPNAME,
				Context.MODE_WORLD_READABLE);
		String station = prefs.getString(Utils.STOREDSTATION, "");

		indexView.setText(bus.getIndex() + "");

		String name = bus.getName();
		Spanned url = Html.fromHtml("<a href=" + bus.getLink() + "\">" + name
				+ "</a>");
		nameView.setText(url);

		if (station.equals(name)) {
			stationLayout.setBackgroundDrawable(mContext.getResources()
					.getDrawable(R.drawable.header));
		}

		nameView.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String url = ((TextView) v).getUrls()[0].getURL();
				String urlString = url.substring(0, url.length() - 1);
				if (!urlString.isEmpty()) {
					Intent busIntent = new Intent(mContext, LearWebView.class);
					busIntent.putExtra(Utils.BROWSE, urlString);
					busIntent.putExtra(Utils.BROWSER, false);
					mContext.startActivity(busIntent);
				}
			}
		});

		nameView.setMovementMethod(LinkMovementMethod.getInstance());
		stripUnderlines(nameView);

		timeView.setText(bus.getBusCount());
		timeView.setTextColor(0xFFFFA544);

		return convertView;
	}

	private void stripUnderlines(TextView textView) {
		Spannable s = (Spannable) textView.getText();
		URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
		for (URLSpan span : spans) {
			int start = s.getSpanStart(span);
			int end = s.getSpanEnd(span);
			s.removeSpan(span);
			span = new URLSpanNoUnderline(span.getURL());
			s.setSpan(span, start, end, 0);
		}
		textView.setText(s);
	}
}
